﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using pttk_htt_hr.DAO;
using pttk_htt_hr.Constant;
using pttk_htt_hr.DTO;
using pttk_htt_hr.Utility;

namespace pttk_htt_hr.GUI
{
    public partial class MH_DanhSachThucDon_TaoSua : Form
    {
        #region Properties
        private string _MaPLTDTT;
        private int _ActionKey;

        public string MaPLTD
        {
            get { return _MaPLTDTT; }
            set { _MaPLTDTT = value; }
        }

        public int ActionKey
        {
            get { return _ActionKey; }
            set { _ActionKey = value; }
        }
        public static int EXEC_OK = 0;

        MH_ThemThucDon MH_ThemThucDon;
        public List<Dictionary<string, string>> ExtractedData;
        #endregion

        #region Initial
        public MH_DanhSachThucDon_TaoSua(string _MaPLTDTT, int _ActionKey)
        {
            InitializeComponent();

            // set args
            this.MaPLTD = _MaPLTDTT;
            this.ActionKey = _ActionKey;
        }
        #endregion

        #region Method
        void create_edit()
        {
            /*
            *  Make column headerId
            */

            string[] ArraHeaderName = new string[] { TableColumnMapping.MaTD, TableColumnMapping.SOLUONG };

            /*
             *  Prepare data source 
             */

            string GhiChu = rtxtGhiChu.Text;
            List<Dictionary<string, string>> DataSource = DataGridUtil.ExtractData_FromDataGridView(dgvThucDon, ArraHeaderName);

            bool PassedAllMadantory = true;
            // Check user input
            for (int i = 0; i < DataSource.Count; i++)
            {
                Dictionary<string, string> row = new Dictionary<string, string>();
                row = DataSource.ElementAt(i);

                if ((row[TableColumnMapping.SOLUONG] == null) || (row[TableColumnMapping.SOLUONG].ToString().Equals("")))
                {
                    PassedAllMadantory = false;
                }
            }



            if (ActionKeyConstant.ACTION_KEY_CREATE == ActionKey)
            {
                if (PassedAllMadantory)
                {
                    /*
                     *  Make parameter
                     */

                    // param for PLTDTTparams sp
                    SqlParameter GhiChuparam = new SqlParameter(ParameterConstant.GHICHU, rtxtGhiChu.Text);

                    List<SqlParameter> PLTDTTparams = new List<SqlParameter>();
                    PLTDTTparams.Add(GhiChuparam);

                    // param for CTPLTDTTparams sp
                    int NextID = ThucDonTuanToiBUS.GetNextMaPLTDTT();

                    ArraHeaderName = new string[] { TableColumnMapping.MaTD, TableColumnMapping.SOLUONG };
                    List<List<SqlParameter>> CTPLTDTTparams = SqlParameterUitl.MakeList_SqlParameter(DataSource, ArraHeaderName);
                    CTPLTDTTparams = SqlParameterUitl.Add_SqlParameter_To_ListSqlParameter(CTPLTDTTparams, TableColumnMapping.MaPLTDTT, Convert.ToString(NextID));

                    // param for delete sp
                    SqlParameter MaPLTDTT = new SqlParameter(TableColumnMapping.MaPLTDTT, Convert.ToString(NextID));
                    List<SqlParameter> Deleteparams = new List<SqlParameter>();
                    Deleteparams.Add(MaPLTDTT);
                    /*
                     * Insert
                     * 
                     */

                    if (EXEC_OK == ThucDonTuanToiBUS.Them_ThucDonTuanToi(PLTDTTparams, CTPLTDTTparams, Deleteparams)) // insert OK
                    {
                        // Call View
                        MH_DanhSachThucDonTuanToi_Xem Xem_dstdtt = new MH_DanhSachThucDonTuanToi_Xem(Convert.ToString(NextID)); // Has Load data
                        Xem_dstdtt.Show();

                        // close current window
                        this.Close();
                    }
                }// End check madatory
                else
                {
                    Dialog.Show_Info_MessageBox_MadatoryNotPassed();

                }
            }

            else if (ActionKeyConstant.ACTION_KEY_EDIT == ActionKey)
            {

                if (PassedAllMadantory)
                {

                    string MaPLTDTT = lblMaPLTDTT.Text;

                    // Param for PLTDTTparams
                    List<SqlParameter> PLTDTTparams = new List<SqlParameter>();
                    PLTDTTparams.Add(new SqlParameter(ParameterConstant.GHICHU, rtxtGhiChu.Text));
                    PLTDTTparams.Add(new SqlParameter(ParameterConstant.MaPLTDTT, MaPLTDTT));




                    ArraHeaderName = new string[] { TableColumnMapping.MaPLTDTT, TableColumnMapping.MaTD, TableColumnMapping.SOLUONG };
                    List<List<SqlParameter>> CTPLTDTTparams = SqlParameterUitl.MakeList_SqlParameter(DataSource, ArraHeaderName);
                    SqlParameterUitl.Add_SqlParameter_To_ListSqlParameter(CTPLTDTTparams, TableColumnMapping.MaPLTDTT, MaPLTDTT);

                    //Tham số phải có @
                    int k = ThucDonTuanToiBUS.CapNhat_ThucDonTuanToi(CTPLTDTTparams, PLTDTTparams, MaPLTDTT, new[] { ParameterConstant.MaTD, ParameterConstant.MaPLTDTT });

                    if (EXEC_OK == k) // insert OK
                    {
                        // Call View
                        MH_DanhSachThucDonTuanToi_Xem Xem_dstdtt = new MH_DanhSachThucDonTuanToi_Xem(MaPLTDTT); // Has Load data
                        Xem_dstdtt.Show();

                        // close current window
                        this.Close();
                    }
                }
                else
                {
                    Dialog.Show_Info_MessageBox_MadatoryNotPassed();
                }
            }
        }
        void load()
        {
            // check action key
            if (ActionKeyConstant.ACTION_KEY_CREATE == ActionKey)
            {

            }
            else if (ActionKeyConstant.ACTION_KEY_EDIT == ActionKey)
            {
                btnTaoSuaDanhSach.Text = ActionKeyConstant.ACTION_NAME_SAVE;

                ThucDonTuanToiDTO tdtd;

                /*
                 *  Set up parameters
                 */

                // List<SqlParameter> PLTDTTparams
                List<SqlParameter> PLTDTTparams = new List<SqlParameter>();
                PLTDTTparams.Add(new SqlParameter(TableColumnMapping.MaPLTDTT, this.MaPLTD));

                // List<SqlParameter> CTPLTDTTparams
                List<SqlParameter> CTPLTDTTparams = new List<SqlParameter>();
                CTPLTDTTparams.Add(new SqlParameter(ParameterConstant.MaPLTDTT, this.MaPLTD));

                /*
                 *  Execute
                 */
                tdtd = ThucDonTuanToiBUS.Lay_ThucDonTuanToi(PLTDTTparams, CTPLTDTTparams);
                //tdtt.PhieuLapThucDonTuanToi.MaPLTDTT

                this.lblMaPLTDTT.Text = tdtd.PhieuLapThucDonTuanToi.MaPLTDTT;
                this.dpNgayTao.Text = tdtd.PhieuLapThucDonTuanToi.NgayLap;
                this.rtxtGhiChu.Text = tdtd.PhieuLapThucDonTuanToi.GhiChu;

                // Add data to gridview
                DataGridUtil.AddData_To_DataGridView(tdtd.ListChiTietPhieuLapThucDonTuanToi, this.dgvThucDon, true, DataGridUtil.GetAll_ColumnName_DataGridView(this.dgvThucDon).ToArray());
            }
        }

        void grid_payback_handel()
        {
            ExtractedData = MH_ThemThucDon.ExtractedDatas;
            
            //ArrHeaderName = MaTD, TenTD, SoLuong; MainKeys = MaTD
            List<Dictionary<string, string>> DataSource = DataGridUtil.grid_data_payback_handle(dgvThucDon, ExtractedData, new[] { TableColumnMapping.MaTD, TableColumnMapping.TenTD,TableColumnMapping.SOLUONG }, new[] { TableColumnMapping.MaTD });

            //Show data
            dgvThucDon.Rows.Clear();

            //ArrHeaderName = STT, MaTD, TenTD, SoLuong;
            DataGridUtil.AddData_To_DataGridView(DataSource, dgvThucDon, true, new[] { TableColumnMapping.STT, TableColumnMapping.MaTD, TableColumnMapping.TenTD, TableColumnMapping.SOLUONG });
        }
        #endregion

        #region Event
        private void MH_TaoDanhSachThucDonTuanToi_btnThemThucDon_Click(object sender, EventArgs e)
        {
            MH_ThemThucDon = new MH_ThemThucDon();
            MH_ThemThucDon.Show();

            MH_ThemThucDon.SubmitClicked += new EventHandler(ThemThucDon_childWindow_SubmitClicked);

        }

        /// <summary>
        /// Mục đích: Lấy dữ liệu từ màn hình Thêm Thực Đơn (màn hình sub)
        /// Bước 1. MH_TaoDanhSachThucDon show MH_ThemThucDon
        /// Bước 2. MH_ThemThucDon close and pass data to MH_TaoDanhSachThucDon
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThemThucDon_childWindow_SubmitClicked(object sender, EventArgs e)
        {
            grid_payback_handel();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (Dialog.Show_MessageBox_YesNo_Close())
            {
                this.Close();
            }

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (Dialog.Show_MessageBox_YesNo_Delete())
            {
                DataGridUtil.DeleteCurrentRowSelected(dgvThucDon);
            }
        }

        private void btnTaoDanhSach_Click(object sender, EventArgs e)
        {
            if (Dialog.Show_MessageBox_YesNo_Save())
            {
                create_edit();
            }
        }

        private void MH_DanhSachThucDon_TaoSua_Load(object sender, EventArgs e)
        {
            load();
        }
        #endregion
    }
}
